package com.wolf.controller;

import com.wolf.pojo.Wl_activity_commodity;
import com.wolf.service.Wl_activity_commodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class Wl_activity_commodityController {

    @Autowired
    Wl_activity_commodityService wl_activity_commodityService;

    /**
     * 查询所有
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_activity_commodity_getAll")
    public String getAll(ModelMap modelMap){
        //查询活动状态
        List<Wl_activity_commodity> getByStatuslist = wl_activity_commodityService.getByStatus();
        modelMap.addAttribute("wl_activity_commodity_getByStatus",getByStatuslist);
        //查询是否包邮
        List<Wl_activity_commodity> getByhasFreightlist = wl_activity_commodityService.getByhasFreight();
        modelMap.addAttribute("wl_activity_commodity_getByhasFreight",getByhasFreightlist);
        //查询活动类型
        List<Wl_activity_commodity> getByTypelist = wl_activity_commodityService.getByType();
        modelMap.addAttribute("wl_activity_commodity_getByType",getByTypelist);
        List<Wl_activity_commodity> list = wl_activity_commodityService.getAll();
        modelMap.addAttribute("wl_activity_commodity_getList",list);
        return "table_Wl_activity_commodity";
    }

    /**
     * 模糊查询
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_activity_commodity_fuzzyQuery")
    public String fuzzyQuery(ModelMap modelMap,Integer hasfreight,Integer type,Integer status,String starttime,String expiredtime){
        //查询活动状态
        List<Wl_activity_commodity> getByStatuslist = wl_activity_commodityService.getByStatus();
        modelMap.addAttribute("wl_activity_commodity_getByStatus",getByStatuslist);
        //查询是否包邮
        List<Wl_activity_commodity> getByhasFreightlist = wl_activity_commodityService.getByhasFreight();
        modelMap.addAttribute("wl_activity_commodity_getByhasFreight",getByhasFreightlist);
        //查询活动类型
        List<Wl_activity_commodity> getByTypelist = wl_activity_commodityService.getByType();
        modelMap.addAttribute("wl_activity_commodity_getByType",getByTypelist);
        //得到结果返回前台
        List<Wl_activity_commodity> list = wl_activity_commodityService.fuzzyQuery(hasfreight,type,status,starttime,expiredtime);
        modelMap.addAttribute("wl_activity_commodity_getList",list);
        return "table_Wl_activity_commodity";
    }

    /**
     * 修改
     */
    @RequestMapping("wl_activity_commodity_getById")
    public String getById(ModelMap modelMap,String id){
        Wl_activity_commodity wl = wl_activity_commodityService.getById(id);
        modelMap.addAttribute("wl_activity_commodity_ById",wl);
        return "wl_activity_commodity_Update";
    }
    @RequestMapping("wl_activity_commodityUpdate")
    public String update(Wl_activity_commodity wl_activity_commodity){
        int num = wl_activity_commodityService.updateByPrimaryKeySelective(wl_activity_commodity);
        if(num>0){
            return "redirect:wl_activity_commodity_getAll";
        }else{
            return "500";
        }
    }

    /**
     * 新增
     */
    @RequestMapping("wl_activity_commodityInsert")
    public String insert(Wl_activity_commodity wl_activity_commodity){
        //设置主键的值 值为当前日期
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String now_date = df.format(new Date());
        wl_activity_commodity.setId(now_date);
        int num = wl_activity_commodityService.insertSelective(wl_activity_commodity);
        if(num>0){
            return "redirect:wl_activity_commodity_getAll";
        }else{
            return "500";
        }
    }

    /**
     * 删除
     */
    @RequestMapping("wl_activity_commodityRemove")
    public String remove(String id){
        int num = wl_activity_commodityService.remove(id);
        if(num>0){
            return "redirect:wl_activity_commodity_getAll";
        }else{
            return "500";
        }
    }
}
